package com.lyubia.shop.rpc.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.lyubia.shop.rpc.mapper.GoodsCategoryMapper;
import com.lyubia.shop.api.pojo.GoodsCategory;
import com.lyubia.shop.api.pojo.GoodsCategoryExample;
import com.lyubia.shop.api.service.GoodsCategoryService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service(version = "1.0", interfaceClass = GoodsCategoryService.class)
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Resource(name = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> valueOperations;

    @Value("${goods.category.list.key}")
    private String redisKey;

    @Override
    public List<GoodsCategory> selectCategoryAndChild() {

        System.out.println("有人在偷偷调用我RPC的方法!!!!!!");

        List<GoodsCategory> res = null;
        if (redisTemplate.hasKey(redisKey)) {
            res = (List<GoodsCategory>) valueOperations.get(redisKey);
            if (CollUtil.isNotEmpty(res))
                System.out.println("偷的我Redis里的数据!!!!!");
                return res;
        }

        GoodsCategoryExample example = new GoodsCategoryExample();
        List<GoodsCategory> categoryList = goodsCategoryMapper.selectByExample(example);
        res =  categoryList.stream()
                .filter(goodsCategory -> goodsCategory.getParentId() == (short) 0)
                .map(goodsCategory -> {
                    goodsCategory.setChildList(selectChildCategory(goodsCategory, categoryList));
                    return goodsCategory;
                })
                .sorted(Comparator.comparingInt(GoodsCategory::getSortOrder))
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(res)) {
            valueOperations.set(redisKey, res);
        }
        System.out.println("偷的我Mysql里的数据!!!!!");
        return res;

    }


    /**
     * 获取目标商品分类的子分类
     * @param temp 目标商品分类
     * @param all 全部分类
     * @return 子分类
     */
    private List<GoodsCategory> selectChildCategory(GoodsCategory temp, List<GoodsCategory> all) {
        return all.stream()
                .filter(goodsCategory -> goodsCategory.getParentId().equals(temp.getId()))
                .map(goodsCategory -> {
                    goodsCategory.setChildList(selectChildCategory(goodsCategory,all));
                    return goodsCategory;
                })
                .sorted(Comparator.comparingInt(GoodsCategory::getSortOrder))
                .collect(Collectors.toList());
    }
}
